<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container"></div>
      <el-form-item prop="account">
        <span class="svg-container"><span class="svg-container-user"></span></span>
        <el-input
          ref="account"
          :maxlength="20"
          v-model="loginForm.account"
          placeholder="账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="pwd">
       <span class="svg-container">
         <span class="svg-container-pass"></span>
       </span>
        <el-input
          :maxlength="20"
          :key="passwordType"
          ref="password"
          v-model="loginForm.pwd"
          :type="passwordType"
          placeholder="密码"
          name="pwd"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" :class="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPwd"></span>
      </el-form-item>

      <!--      <el-button-->
      <!--        :loading="loading"-->
      <!--        type="primary"-->
      <!--        style="width: 100%; margin-bottom: 30px"-->
      <!--        @click.native.prevent="handleLogin"-->
      <!--        >登录</el-button>-->
      <div class="loginBtn" @click="handleLogin"></div>

      <!--      <div class="tips">-->
      <!--        <span style="margin-right:20px;">username: admin</span>-->
      <!--        <span> password: any</span>-->
      <!--      </div>-->
    </el-form>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import {getToken} from "@/utils/auth";

export default {
  name: 'Login',
  computed: {
    ...mapGetters(['token'])
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        account: '',
        pwd: ''
      },
      loginRules: {
        account: [
          {required: true, trigger: 'blur', validator: validateUsername}
        ],
        pwd: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #004cff;
;
$cursor: #004cff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui scss */
.login-container {
  .el-input {
    display: inline-block;
    width: 85%;
    //line-height: 70px;

    input {
      background-color: transparent;
      background-image: none;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 25px;
      color: $light_gray;
      font-weight: bold;
      font-size: 24px;
      caret-color: $cursor;

      &:-webkit-autofill::first-line {
        font-size: 24px;
      }

      &:-webkit-autofill {
        //box-shadow: 0 0 0px 1000px transparent inset !important;
        //-webkit-text-fill-color: $light_gray !important;
        -webkit-text-fill-color: $light_gray !important;
        transition: background-color 5000s ease-in-out 0s;
      }

      &:-webkit-autofill::first-line {
        font-size: 24px;
      }
    }
  }

  .el-form-item {
    border-bottom: 1px solid #0073ff;
    //background: rgba(0, 0, 0, 0.1);
    //border-radius: 5px;
    color: #004cff;
  }
}
</style>

<style lang="scss" scoped>
$bg: 'url()';
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background: url("~@/assets/img/login/bg.png") no-repeat;
  background-size: cover;
  overflow: hidden;

  .login-form {
    width: 448px;
    width: 448px;
    position: absolute;
    top: 50%;
    left: 32%;
    transform: translate(-50%, -50%);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    vertical-align: middle;
    height: 70px;
    line-height: 70px;
    display: inline-block;
  }

  .svg-container-user {
    width: 32px;
    height: 32px;
    background: url("~@/assets/img/login/user.png") no-repeat;
    background-size: 100% 100%;
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }

  .svg-container-pass {
    width: 32px;
    height: 32px;
    background: url("~@/assets/img/login/pass.png") no-repeat;
    background-size: 100% 100%;
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }

  .title-container {
    position: relative;
    width: 432px;
    height: 51px;
    background: url("~@/assets/img/login/title.png") no-repeat;
    background-size: 100% 100%;
    margin-bottom: 100px;
  }

  .show-pwd {
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    width: 32px;
    height: 32px;
    display: inline-block;
  }

  .eye {
    position: absolute;
    right: 10px;
    top: 30px;
    width: 26px;
    height: 14px;

    background: url("~@/assets/img/login/hide.png") no-repeat;
    background-size: 100% 100%;
  }

  .eye-open {
    position: absolute;
    right: 10px;
    top: 23px;
    width: 30px;
    height: 22px;

    background: url("~@/assets/img/login/open.png") no-repeat;
    background-size: 100% 100%;
  }

  .loginBtn {
    cursor: pointer;
    margin-top: 100px;
    width: 231px;
    height: 62px;
    background: url("~@/assets/img/login/password.png") no-repeat;
    background-size: 100% 100%;
  }
}
</style>
